/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import blueprint4j.utils.*;

import java.math.*;
import java.text.*;
import javax.swing.*;

public class DataPairFieldCurrency extends DataPairDefault implements FieldBinding {
    private BindCurrencyInterface field;
    private JFormattedTextField text_field = new JFormattedTextField(new DecimalFormat(Settings.getString("gui.gui.db_tools.curency_format", "###,###,##0.00;-###,###,##0.00")));;
    
    public DataPairFieldCurrency(Binder binder,BindCurrencyInterface p_field) {
        this(binder,p_field, true);
    }
    
    public DataPairFieldCurrency(Binder binder,BindCurrencyInterface p_field, boolean editable) {
        super(binder,p_field);
        text_field.setColumns(10);
        text_field.setHorizontalAlignment(JTextField.RIGHT);
        
        field = p_field;
        setLabel(new JLabel(field.getDescription()));
        setComponent(text_field);
        text_field.setEditable(editable);
    }
    
    public boolean isChanged() {
        Currency value = field.get();
        if (value == null) {
            return text_field.getText() != null;
        } else {
            return text_field.getText() != null && !value.equals(new Currency(new BigDecimal(text_field.getText())));
        }
    }
    
    public void intoControl() {
        if (field.get() == null) {
            //text_field.setValue(new BigDecimal("0"));
            text_field.setValue(null);
        } else {
            Currency value=field.get();
            if (value != null) {
                text_field.setValue(value.getBigDecimal());//new BigDecimal(value.replaceAll(",","")));
            }else{
                text_field.setValue(new BigDecimal("0"));
            }
        }
    }
    
    public void intoBindable() {
        String value=text_field.getText();
        if (value != null && value.length() > 0) {
            field.set(new Currency(new BigDecimal(value.replaceAll(",",""))));
        } else {
            field.set(null);
        }
    }
    
    public Bindable getBindable() {
        return field.getBindable();
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
        field = (BindCurrencyInterface) p_field;
    }
    
    public void resetControl() {
        text_field.setText("");
    }
}

